package cn.zjxiliu.lib_login.widget;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import cn.zjxiliu.lib_core.widget.ClearEditText;
import cn.zjxiliu.lib_login.R;
import cn.zjxiliu.lib_login.utils.CountDownTimerUtils;

/**
 * Created by jessie on 2018/11/21.
 */

public class VerificationCodeInputView extends LinearLayout implements View.OnClickListener{

    private LinearLayout inputVerificationCodeView;

    private ClearEditText etVerifyCode;
    private View viewLine;
    private TextView tvVerifyText;


    private InputVerificationClickListener listener;

    /**
     * 展现倒计时
     */
    public void startCountDownTimer(){
        CountDownTimerUtils countDownTimer = new CountDownTimerUtils(tvVerifyText, 60000, 1000);
        countDownTimer.start();
    }

    public VerificationCodeInputView(Context context) {
        super(context);
    }

    public VerificationCodeInputView(Context context, AttributeSet attrs) {
        super(context, attrs);
        inputVerificationCodeView = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.input_verificationcode_view, null);
        etVerifyCode = (ClearEditText) inputVerificationCodeView.findViewById(R.id.etVerifyCode);
        tvVerifyText = (TextView) inputVerificationCodeView.findViewById(R.id.tvVerifyText);
        viewLine = inputVerificationCodeView.findViewById(R.id.viewLine);
        initClick();
        initListen();
        addView(inputVerificationCodeView);

    }


    private void initClick() {
        tvVerifyText.setOnClickListener(this);
    }

    private void initListen() {

        etVerifyCode.setTextChangeListener(new ClearEditText.OnTextChangeListener() {
            @Override
            public void onFocusChange(boolean hasFocus) {
                if (hasFocus) {
                    viewLine.setBackgroundColor(getResources().getColor(R.color.c3F86FF));
                    if (listener != null) {
                        listener.onFocusChange(hasFocus);
                    }
                } else {
                    viewLine.setBackgroundColor(getResources().getColor(R.color.cDBDBDB));
                    if (listener != null) {
                        listener.onFocusChange(hasFocus);
                    }
                }
            }

            @Override
            public void afterTextChanged(String s) {
                if (TextUtils.isEmpty(s)) {
                    if (listener != null) {
                        listener.inputViewHasText(false);
                    }
                } else if(s.length() == 6){
                    if (listener != null) {
                        listener.inputViewHasText(true);
                    }
                }else{
                    if (listener != null) {
                        listener.inputViewHasText(false);
                    }
                }

            }
        });
    }


    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.tvVerifyText) {
            if(listener != null){
                listener.onVerificationBtnClick();
            }
        }
    }



    public void setVerificationBtnClickListener(InputVerificationClickListener listener) {
        this.listener = listener;

    }

    public String getText() {
        return etVerifyCode.getText().toString().trim().replaceAll(" ", "");
    }


    public void clear(){
        etVerifyCode.setText("");
    }

    public VerificationCodeInputView setHintText(String text) {
        etVerifyCode.setHint(text);
        return this;
    }

    public VerificationCodeInputView setInputType(int type) {
        etVerifyCode.setInputType(type);
        return this;
    }

    public VerificationCodeInputView isHideUnderline(boolean isHide) {
        if (isHide) {
            etVerifyCode.setVisibility(GONE);
        } else {
            etVerifyCode.setVisibility(VISIBLE);
        }
        return this;
    }

    public VerificationCodeInputView setHintColor(int color) {
        etVerifyCode.setHintTextColor(color);
        return this;
    }

    public VerificationCodeInputView setTextSize(float size) {
        etVerifyCode.setTextSize(size);
        return this;
    }

    public VerificationCodeInputView setTextColor(int color) {
        etVerifyCode.setTextColor(color);
        return this;
    }

    public VerificationCodeInputView setMaxLength(int maxLength) {
        etVerifyCode.setFilters(new InputFilter[] { new InputFilter.LengthFilter(maxLength) });
        return this;
    }

    public VerificationCodeInputView setDrawableLeft(Drawable drawableLeft) {
        etVerifyCode.setDrawableLeft(drawableLeft);
        return this;
    }




}
